package com.wqm.cn.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
/**
*
*  @author wqm
*/
public class REPORTDATASOURCE implements Serializable {

    private static final long serialVersionUID = 1607691489402L;


    /**
    * 主键
    * 数据源ID
    * isNullAble:0
    */
    private Integer ID;

    /**
    * 数据源名字
    * isNullAble:0,defaultVal:Current
    */
    private String dATASOURCENAME;

    /**
    * 数据源使用的表或SQL语句
    * isNullAble:0
    */
    private String sQLTABLE;


    public void setID(Integer ID){this.ID = ID;}

    public Integer getID(){return this.ID;}

    public void setDATASOURCENAME(String dATASOURCENAME){this.dATASOURCENAME = dATASOURCENAME;}

    public String getDATASOURCENAME(){return this.dATASOURCENAME;}

    public void setSQLTABLE(String sQLTABLE){this.sQLTABLE = sQLTABLE;}

    public String getSQLTABLE(){return this.sQLTABLE;}
    @Override
    public String toString() {
        return "REPORTDATASOURCE{" +
                "ID='" + ID + '\'' +
                "dATASOURCENAME='" + dATASOURCENAME + '\'' +
                "sQLTABLE='" + sQLTABLE + '\'' +
            '}';
    }

    public static Builder Build(){return new Builder();}

    public static ConditionBuilder ConditionBuild(){return new ConditionBuilder();}

    public static UpdateBuilder UpdateBuild(){return new UpdateBuilder();}

    public static QueryBuilder QueryBuild(){return new QueryBuilder();}

    public static class UpdateBuilder {

        private REPORTDATASOURCE set;

        private ConditionBuilder where;

        public UpdateBuilder set(REPORTDATASOURCE set){
            this.set = set;
            return this;
        }

        public REPORTDATASOURCE getSet(){
            return this.set;
        }

        public UpdateBuilder where(ConditionBuilder where){
            this.where = where;
            return this;
        }

        public ConditionBuilder getWhere(){
            return this.where;
        }

        public UpdateBuilder build(){
            return this;
        }
    }

    public static class QueryBuilder extends REPORTDATASOURCE{
        /**
        * 需要返回的列
        */
        private Map<String,Object> fetchFields;

        public Map<String,Object> getFetchFields(){return this.fetchFields;}

        private List<Integer> IDList;

        public List<Integer> getIDList(){return this.IDList;}

        private Integer IDSt;

        private Integer IDEd;

        public Integer getIDSt(){return this.IDSt;}

        public Integer getIDEd(){return this.IDEd;}

        private List<String> dATASOURCENAMEList;

        public List<String> getDATASOURCENAMEList(){return this.dATASOURCENAMEList;}


        private List<String> fuzzyDATASOURCENAME;

        public List<String> getFuzzyDATASOURCENAME(){return this.fuzzyDATASOURCENAME;}

        private List<String> rightFuzzyDATASOURCENAME;

        public List<String> getRightFuzzyDATASOURCENAME(){return this.rightFuzzyDATASOURCENAME;}
        private List<String> sQLTABLEList;

        public List<String> getSQLTABLEList(){return this.sQLTABLEList;}


        private List<String> fuzzySQLTABLE;

        public List<String> getFuzzySQLTABLE(){return this.fuzzySQLTABLE;}

        private List<String> rightFuzzySQLTABLE;

        public List<String> getRightFuzzySQLTABLE(){return this.rightFuzzySQLTABLE;}
        private QueryBuilder (){
            this.fetchFields = new HashMap<>();
        }

        public QueryBuilder IDBetWeen(Integer IDSt,Integer IDEd){
            this.IDSt = IDSt;
            this.IDEd = IDEd;
            return this;
        }

        public QueryBuilder IDGreaterEqThan(Integer IDSt){
            this.IDSt = IDSt;
            return this;
        }
        public QueryBuilder IDLessEqThan(Integer IDEd){
            this.IDEd = IDEd;
            return this;
        }


        public QueryBuilder ID(Integer ID){
            setID(ID);
            return this;
        }

        public QueryBuilder IDList(Integer ... ID){
            this.IDList = solveNullList(ID);
            return this;
        }

        public QueryBuilder IDList(List<Integer> ID){
            this.IDList = ID;
            return this;
        }

        public QueryBuilder fetchID(){
            setFetchFields("fetchFields","ID");
            return this;
        }

        public QueryBuilder excludeID(){
            setFetchFields("excludeFields","ID");
            return this;
        }

        public QueryBuilder fuzzyDATASOURCENAME (List<String> fuzzyDATASOURCENAME){
            this.fuzzyDATASOURCENAME = fuzzyDATASOURCENAME;
            return this;
        }

        public QueryBuilder fuzzyDATASOURCENAME (String ... fuzzyDATASOURCENAME){
            this.fuzzyDATASOURCENAME = solveNullList(fuzzyDATASOURCENAME);
            return this;
        }

        public QueryBuilder rightFuzzyDATASOURCENAME (List<String> rightFuzzyDATASOURCENAME){
            this.rightFuzzyDATASOURCENAME = rightFuzzyDATASOURCENAME;
            return this;
        }

        public QueryBuilder rightFuzzyDATASOURCENAME (String ... rightFuzzyDATASOURCENAME){
            this.rightFuzzyDATASOURCENAME = solveNullList(rightFuzzyDATASOURCENAME);
            return this;
        }

        public QueryBuilder dATASOURCENAME(String dATASOURCENAME){
            setDATASOURCENAME(dATASOURCENAME);
            return this;
        }

        public QueryBuilder dATASOURCENAMEList(String ... dATASOURCENAME){
            this.dATASOURCENAMEList = solveNullList(dATASOURCENAME);
            return this;
        }

        public QueryBuilder dATASOURCENAMEList(List<String> dATASOURCENAME){
            this.dATASOURCENAMEList = dATASOURCENAME;
            return this;
        }

        public QueryBuilder fetchDATASOURCENAME(){
            setFetchFields("fetchFields","dATASOURCENAME");
            return this;
        }

        public QueryBuilder excludeDATASOURCENAME(){
            setFetchFields("excludeFields","dATASOURCENAME");
            return this;
        }

        public QueryBuilder fuzzySQLTABLE (List<String> fuzzySQLTABLE){
            this.fuzzySQLTABLE = fuzzySQLTABLE;
            return this;
        }

        public QueryBuilder fuzzySQLTABLE (String ... fuzzySQLTABLE){
            this.fuzzySQLTABLE = solveNullList(fuzzySQLTABLE);
            return this;
        }

        public QueryBuilder rightFuzzySQLTABLE (List<String> rightFuzzySQLTABLE){
            this.rightFuzzySQLTABLE = rightFuzzySQLTABLE;
            return this;
        }

        public QueryBuilder rightFuzzySQLTABLE (String ... rightFuzzySQLTABLE){
            this.rightFuzzySQLTABLE = solveNullList(rightFuzzySQLTABLE);
            return this;
        }

        public QueryBuilder sQLTABLE(String sQLTABLE){
            setSQLTABLE(sQLTABLE);
            return this;
        }

        public QueryBuilder sQLTABLEList(String ... sQLTABLE){
            this.sQLTABLEList = solveNullList(sQLTABLE);
            return this;
        }

        public QueryBuilder sQLTABLEList(List<String> sQLTABLE){
            this.sQLTABLEList = sQLTABLE;
            return this;
        }

        public QueryBuilder fetchSQLTABLE(){
            setFetchFields("fetchFields","sQLTABLE");
            return this;
        }

        public QueryBuilder excludeSQLTABLE(){
            setFetchFields("excludeFields","sQLTABLE");
            return this;
        }
        private <T>List<T> solveNullList(T ... objs){
            if (objs != null){
            List<T> list = new ArrayList<>();
                for (T item : objs){
                    if (item != null){
                        list.add(item);
                    }
                }
                return list;
            }
            return null;
        }

        public QueryBuilder fetchAll(){
            this.fetchFields.put("AllFields",true);
            return this;
        }

        public QueryBuilder addField(String ... fields){
            List<String> list = new ArrayList<>();
            if (fields != null){
                for (String field : fields){
                    list.add(field);
                }
            }
            this.fetchFields.put("otherFields",list);
            return this;
        }
        @SuppressWarnings("unchecked")
        private void setFetchFields(String key,String val){
            Map<String,Boolean> fields= (Map<String, Boolean>) this.fetchFields.get(key);
            if (fields == null){
                fields = new HashMap<>();
            }
            fields.put(val,true);
            this.fetchFields.put(key,fields);
        }

        public REPORTDATASOURCE build(){return this;}
    }


    public static class ConditionBuilder{
        private List<Integer> IDList;

        public List<Integer> getIDList(){return this.IDList;}

        private Integer IDSt;

        private Integer IDEd;

        public Integer getIDSt(){return this.IDSt;}

        public Integer getIDEd(){return this.IDEd;}

        private List<String> dATASOURCENAMEList;

        public List<String> getDATASOURCENAMEList(){return this.dATASOURCENAMEList;}


        private List<String> fuzzyDATASOURCENAME;

        public List<String> getFuzzyDATASOURCENAME(){return this.fuzzyDATASOURCENAME;}

        private List<String> rightFuzzyDATASOURCENAME;

        public List<String> getRightFuzzyDATASOURCENAME(){return this.rightFuzzyDATASOURCENAME;}
        private List<String> sQLTABLEList;

        public List<String> getSQLTABLEList(){return this.sQLTABLEList;}


        private List<String> fuzzySQLTABLE;

        public List<String> getFuzzySQLTABLE(){return this.fuzzySQLTABLE;}

        private List<String> rightFuzzySQLTABLE;

        public List<String> getRightFuzzySQLTABLE(){return this.rightFuzzySQLTABLE;}

        public ConditionBuilder IDBetWeen(Integer IDSt,Integer IDEd){
            this.IDSt = IDSt;
            this.IDEd = IDEd;
            return this;
        }

        public ConditionBuilder IDGreaterEqThan(Integer IDSt){
            this.IDSt = IDSt;
            return this;
        }
        public ConditionBuilder IDLessEqThan(Integer IDEd){
            this.IDEd = IDEd;
            return this;
        }


        public ConditionBuilder IDList(Integer ... ID){
            this.IDList = solveNullList(ID);
            return this;
        }

        public ConditionBuilder IDList(List<Integer> ID){
            this.IDList = ID;
            return this;
        }

        public ConditionBuilder fuzzyDATASOURCENAME (List<String> fuzzyDATASOURCENAME){
            this.fuzzyDATASOURCENAME = fuzzyDATASOURCENAME;
            return this;
        }

        public ConditionBuilder fuzzyDATASOURCENAME (String ... fuzzyDATASOURCENAME){
            this.fuzzyDATASOURCENAME = solveNullList(fuzzyDATASOURCENAME);
            return this;
        }

        public ConditionBuilder rightFuzzyDATASOURCENAME (List<String> rightFuzzyDATASOURCENAME){
            this.rightFuzzyDATASOURCENAME = rightFuzzyDATASOURCENAME;
            return this;
        }

        public ConditionBuilder rightFuzzyDATASOURCENAME (String ... rightFuzzyDATASOURCENAME){
            this.rightFuzzyDATASOURCENAME = solveNullList(rightFuzzyDATASOURCENAME);
            return this;
        }

        public ConditionBuilder dATASOURCENAMEList(String ... dATASOURCENAME){
            this.dATASOURCENAMEList = solveNullList(dATASOURCENAME);
            return this;
        }

        public ConditionBuilder dATASOURCENAMEList(List<String> dATASOURCENAME){
            this.dATASOURCENAMEList = dATASOURCENAME;
            return this;
        }

        public ConditionBuilder fuzzySQLTABLE (List<String> fuzzySQLTABLE){
            this.fuzzySQLTABLE = fuzzySQLTABLE;
            return this;
        }

        public ConditionBuilder fuzzySQLTABLE (String ... fuzzySQLTABLE){
            this.fuzzySQLTABLE = solveNullList(fuzzySQLTABLE);
            return this;
        }

        public ConditionBuilder rightFuzzySQLTABLE (List<String> rightFuzzySQLTABLE){
            this.rightFuzzySQLTABLE = rightFuzzySQLTABLE;
            return this;
        }

        public ConditionBuilder rightFuzzySQLTABLE (String ... rightFuzzySQLTABLE){
            this.rightFuzzySQLTABLE = solveNullList(rightFuzzySQLTABLE);
            return this;
        }

        public ConditionBuilder sQLTABLEList(String ... sQLTABLE){
            this.sQLTABLEList = solveNullList(sQLTABLE);
            return this;
        }

        public ConditionBuilder sQLTABLEList(List<String> sQLTABLE){
            this.sQLTABLEList = sQLTABLE;
            return this;
        }

        private <T>List<T> solveNullList(T ... objs){
            if (objs != null){
            List<T> list = new ArrayList<>();
                for (T item : objs){
                    if (item != null){
                        list.add(item);
                    }
                }
                return list;
            }
            return null;
        }

        public ConditionBuilder build(){return this;}
    }

    public static class Builder {

        private REPORTDATASOURCE obj;

        public Builder(){
            this.obj = new REPORTDATASOURCE();
        }

        public Builder ID(Integer ID){
            this.obj.setID(ID);
            return this;
        }
        public Builder dATASOURCENAME(String dATASOURCENAME){
            this.obj.setDATASOURCENAME(dATASOURCENAME);
            return this;
        }
        public Builder sQLTABLE(String sQLTABLE){
            this.obj.setSQLTABLE(sQLTABLE);
            return this;
        }
        public REPORTDATASOURCE build(){return obj;}
    }

}
